using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace CustomPeds;

public class Decoration
{
    public enum Gender
    {
        GENDER_MALE,
        GENDER_FEMALE,
        GENDER_DONTCARE
    }

    public enum Zone
    {
        ZONE_TORSO,
        ZONE_HEAD,
        ZONE_LEFT_ARM,
        ZONE_RIGHT_ARM,
        ZONE_LEFT_LEG,
        ZONE_RIGHT_LEG,
        ZONE_UNKNOWN,
        ZONE_NONE
    }

    public enum Type
    {
        TYPE_TATTOO,
        TYPE_BADGE,
        TYPE_INVALID
    }

    public enum FACTION
    {
        MICHAEL,
        FRANKLIN,
        TREVOR,
        FM
    }

    public class Collection
    {
        public class Overlay
        {
            public string nameHash;
            public string txtHash;
            public string zone;
            public string type;
            public string faction;
            public string gender;
            public string award;
        }

        public string nameHash;
        public bool haveTattoo;
        public bool haveBadge;
        public List<Overlay> overlay = new List<Overlay>();

        public static Collection GetCollectionClassify(Collection collection, GTA.Gender gender, Zone zone = Zone.ZONE_NONE)
        {
            Collection collection2 = new Collection();
            for (int i = 0; i < collection.overlay.Count; i++)
            {
                collection2.haveBadge = collection.haveBadge;
                collection2.haveTattoo = collection.haveTattoo;
                collection2.nameHash = collection.nameHash;
                if (!(collection.overlay[i].gender == Gender.GENDER_DONTCARE.ToString()))
                {
                    string gender2 = collection.overlay[i].gender;
                    Gender gender3 = (Gender)gender;
                    if (!(gender2 == gender3.ToString()))
                    {
                        continue;
                    }
                }
                if (zone == Zone.ZONE_NONE || collection.overlay[i].zone == zone.ToString())
                {
                    collection2.overlay.Add(collection.overlay[i]);
                }
            }
            return collection2;
        }

        public static List<Collection> GetCollectionList(Type type)
        {
            List<Collection> list = LoadDecorationXMLData();
            List<Collection> list2 = new List<Collection>();
            for (int i = 0; i < list.Count; i++)
            {
                if ((type == Type.TYPE_TATTOO && !list[i].haveTattoo) || (type == Type.TYPE_BADGE && !list[i].haveBadge))
                {
                    continue;
                }
                Collection collection = new Collection();
                collection.nameHash = list[i].nameHash;
                collection.haveTattoo = list[i].haveTattoo;
                collection.haveBadge = list[i].haveBadge;
                for (int j = 0; j < list[i].overlay.Count; j++)
                {
                    if (list[i].overlay[j].type == type.ToString())
                    {
                        collection.overlay.Add(list[i].overlay[j]);
                    }
                }
                list2.Add(collection);
            }
            return list2;
        }
    }

    private static List<Collection> LoadDecorationXMLData()
    {
        List<Collection> list = new List<Collection>();
        List<string> list2 = new List<string>();
        FileInfo[] files = new DirectoryInfo("scripts\\CustomPeds\\Decoration").GetFiles("*.xml", SearchOption.AllDirectories);
        foreach (FileInfo fileInfo in files)
        {
            Collection collection = new Collection();
            Collection.Overlay overlay = new Collection.Overlay();
            if (!IsXml(fileInfo.FullName))
            {
                continue;
            }
            Path.GetDirectoryName(fileInfo.FullName);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileInfo.FullName);
            XmlNode xmlNode = xmlDocument.SelectSingleNode("/PedDecorationCollection/nameHash");
            if (xmlNode != null)
            {
                XmlNodeReader xmlNodeReader = new XmlNodeReader(xmlNode);
                xmlNodeReader.Read();
                xmlNodeReader.Read();
                if (xmlNodeReader.Value == null || xmlNodeReader.Value == string.Empty)
                {
                    continue;
                }
                collection.nameHash = xmlNodeReader.Value;
            }
            XmlNode xmlNode2 = xmlDocument.SelectSingleNode("/PedDecorationCollection/presets");
            if (xmlNode2 != null)
            {
                XmlNodeReader nr = new XmlNodeReader(xmlNode2);
                while (nr.Read())
                {
                    if (nr.NodeType == XmlNodeType.Element && nr.Name == "nameHash")
                    {
                        nr.Read();
                        if (nr.Value == null || nr.Value == string.Empty || list2.FindIndex(hash => hash == nr.Value) >= 0)
                        {
                            while (nr.Read() && (nr.NodeType != XmlNodeType.EndElement || !(nr.Name == "Item")))
                            {
                            }
                            continue;
                        }
                        list2.Add(nr.Value);
                        overlay.nameHash = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "txtHash")
                    {
                        nr.Read();
                        overlay.txtHash = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "zone")
                    {
                        nr.Read();
                        overlay.zone = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "type")
                    {
                        nr.Read();
                        if (nr.Value == "TYPE_TATTOO")
                        {
                            collection.haveTattoo = true;
                        }
                        if (nr.Value == "TYPE_BADGE")
                        {
                            collection.haveBadge = true;
                        }
                        overlay.type = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "faction")
                    {
                        nr.Read();
                        overlay.faction = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "gender")
                    {
                        nr.Read();
                        overlay.gender = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.Element && nr.Name == "award")
                    {
                        nr.Read();
                        overlay.award = nr.Value;
                    }
                    else if (nr.NodeType == XmlNodeType.EndElement && nr.Name == "Item")
                    {
                        collection.overlay.Add(overlay);
                        overlay = new Collection.Overlay();
                    }
                }
            }
            if (collection.nameHash != null && collection.nameHash.Replace(" ", "") != string.Empty)
            {
                list.Add(collection);
            }
        }
        return list;
    }

    private static bool IsXml(string Path)
    {
        try
        {
            new XmlDocument().Load(Path);
            return true;
        }
        catch
        {
            return false;
        }
    }
}


